# @author: Caijiaming
# @Time 2023/9/18 14:22
# @贪吃蛇2

import pygame,sys,random
from pygame.locals import *
from collections import deque

#定义一些常量
screen_width = 600
screen_height = 480
size = 20
scope_x = (0,screen_width // size - 1)
scope_y = (0,screen_height // size - 1)
snake_color = (200,150,150)
bg_color = (0,0,0)
food_color = (200,200,200)

#初始化蛇
def init_snake():
    snake = deque()
    snake.append((2,scope_y[0]))
    snake.append((1,scope_y[0]))
    snake.append((0,scope_y[0]))
    return snake

#初始化食物
def init_food(snake):
    food_x = random.randint(0,scope_x[1])
    food_y = random.randint(0,scope_y[1])
    if (food_x,food_y) in snake:
        food_x = random.randint(0, scope_x[1])
        food_y = random.randint(0, scope_y[1])
    return food_x,food_y

def main():
    pygame.init()
    screen = pygame.display.set_mode((screen_width,screen_height))
    pygame.display.set_caption("贪吃蛇")
    clock = pygame.time.Clock()
    game_over = False
    # 初始化蛇
    snake = init_snake()
    #方向控制
    pos = (1,0)
    b = True
    font = pygame.font.Font(None,72)
    fwidth,fheight = font.size("GAME OVER")
    #初始化食物
    food = init_food(snake)
    #是否暂停
    pause = False

    while True:
        screen.fill(bg_color)

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            if event.type == KEYDOWN:
                #暂停游戏
                if event.key == K_SPACE:
                    pause = not pause
                if event.key in (K_s,K_DOWN):
                    if b and not pos[1]:
                        pos = (0, 1)
                        b = False
                if event.key in (K_w, K_UP):
                    if b and not pos[1]:
                        pos = (0, -1)
                        b = False
                if event.key in (K_a, K_LEFT):
                    if b and not pos[0]:
                        pos = (-1, 0)
                        b = False
                if event.key in (K_d, K_RIGHT):
                    if b and not pos[0]:
                        pos = (1, 0)
                        b = False

        if not game_over:
            if not pause:
                b = True
                next_s = (snake[0][0] + pos[0],snake[0][1] + pos[1])
                #是否吃到食物
                if next_s == food:
                    snake.appendleft(food)
                    food = init_food(snake)
                else:
                    # 判断是否触碰边界
                    if scope_x[0] <= next_s[0] <= scope_x[1] and scope_y[0] <= next_s[1] <= scope_y[1] \
                            and next_s not in snake:
                        snake.appendleft(next_s)
                        snake.pop()
                    else:
                        game_over = True


        #画食物
        if not game_over:
            pygame.draw.rect(screen,food_color,(food[0] * size,food[1]* size,size,size),0)

        #画蛇
        for s in snake:
            pygame.draw.rect(screen,snake_color,(s[0] * size,s[1] * size,size,size),0)

        # 游戏暂停
        if pause:
            font_txt = font.render("GAME PAUSE", True, (255, 255, 255))
            pwidth, pheight = font_txt.get_size()
            screen.blit(font_txt, ((screen_width - pwidth) // 2, (screen_height - pheight) // 2))

        #游戏结束
        if game_over:
            font_txt = font.render("GAME OVER",True,(255,255,255))
            screen.blit(font_txt,((screen_width - fwidth) // 2,(screen_height - fheight) // 2))

        pygame.display.update()
        clock.tick(5)

#运行主函数
if __name__ == '__main__':
    main()

